package deviceconn

import serial.SerialPort

class TTYSerialPort(name: String, baudRate: Int) : SerialPort {

    companion object {
        init {
            System.loadLibrary("serial_rs")
        }
    }

    private val fd: Long

    init {
        fd = open(name, baudRate)
    }

    override fun write(buf: ByteArray) {
        write(fd, buf)
    }

    override fun close() {
        close(fd)
    }

    override fun read(): ByteArray {
        return read(fd)
    }

    private external fun open(name: String, baudRate: Int): Long
    private external fun close(fd: Long)
    private external fun write(fd: Long, buf: ByteArray)
    private external fun read(fd: Long): ByteArray
}
